<style>
</style>

<template>

  <div class="manage-wrap">
    <div class="operation-panel">
      <Row>
        <Col span="24">
        <Poptip
          width="400"
          title='搜索'
          placement="bottom-end"
          class="float-r"
        >
          <Button
            type="primary"
            size="large"
            icon="ios-search"
          >搜 索</Button>
          <div
            style="text-align:center"
            slot="content"
          >
            <Form
              ref="searchForm"
              :model="searchForm"
              :label-width="80"
              value=true
              style="min-width:200px;padding-top:20px;border-top:1px solid #a3adba;border-bottom:1px solid #a3adba;"
            >
              <Row>
                <Form-item label="用户名称">
                  <Input v-model="searchForm.username"></Input>
                </Form-item>
              </Row>
            </Form>
            <Row>
              <Button
                style="margin-left:5px;margin-top:10px;float:right;background-color: #5bc0de;color:#fff"
                size="small"
                @click="doSearchTableList"
              >确定</Button>
              <Button
                style="float:right;margin-top:10px;"
                size="small"
                @click="resetSearch"
              >重置</Button>
            </Row>
          </div>
        </Poptip>
        <Button
          class="float-r"
          size="large"
          icon="md-add"
          @click="addUser"
          style="margin-right:10px;"
        >添加</Button>

        </Col>
      </Row>
    </div>

    <!--table-->
    <Row>
      <Table
        stripe
        size="small"
        :columns="tableColums"
        :data="tableData"
      ></Table>
    </Row>
    <Row>
      <Page
        :total="total"
        :current="currentPage"
        @on-change="changeCurrentPage"
        show-total
        style="float:right;margin-top:10px"
      ></Page>
    </Row>
    <!--新增编辑-->
    <userForm
      :modalShow="formShow"
      :modalFormTitle="formTitle"
      :parentForm="parentForm"
      @listenModalForm="hideModel"
      @refreshTableList="getTableList"
    ></userForm>
    <!--是否删除框-->
    <Modal
      v-model="delModal"
      width="360"
    >
      <p
        slot="header"
        style="color:#f60;text-align:center"
      >
        <Icon type="information-circled"></Icon>
        <span>删除确认</span>
      </p>
      <div style="text-align:center">
        <p>是否继续删除？</p>
      </div>
      <div slot="footer">
        <Button
          type="error"
          size="large"
          long
          :loading="btnLoading"
          @click="comfirmDel"
        >删除</Button>
      </div>
    </Modal>
    <!--是否重置密码框-->
    <Modal
      v-model="resetModal"
      width="360"
    >
      <p
        slot="header"
        style="color:#f60;text-align:center"
      >
        <Icon type="information-circled"></Icon>
        <span>重置确认</span>
      </p>
      <div style="text-align:center">
        <p>重置后该用户密码为123456,是否继续？</p>
      </div>
      <div slot="footer">
        <Button
          type="warning"
          size="large"
          long
          :loading="btnLoading"
          @click="comfirmReset"
        >重置</Button>
      </div>
    </Modal>
  </div>

</template>

<script>
import { getUserList, delUser, resetPwd } from "@/api/system";
import userForm from "./userForm.vue";
export default {
  name: "userManagement",
  components: {
    userForm
  },
  data() {
    return {
      tableColums: [
        {
          type: "index",
          width: 60,
          title: "序号",
          align: "center"
        },
        {
          align: "center",
          title: "用户名",
          key: "username"
        },
        {
          align: "center",
          title: "所属部门",
          key: "deptName"
        },
        {
          align: "center",
          title: "邮箱",
          key: "email"
        },
        {
          align: "center",
          title: "电话",
          key: "mobile"
        },

        {
          align: "center",
          title: "状态",
          key: "status",
          render: (h, params) => {
            const row = params.row;
            return h(
              "Tag",
              {
                props: {
                  type: "dot",
                  color: row.status == 1 ? "success" : "error"
                }
              },
              row.status == 1 ? "正常" : "禁用"
            );
          }
        },
        {
          align: "center",
          title: "创建时间",
          key: "createTime"
        },
        {
          title: "操作",
          align: "center",
          render: (h, params) => {
            let actions = [];

            actions.push(
              h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small"
                  },
                  directives: [
                    {
                      name: "perms",
                      value: "sys:user:update"
                    }
                  ],
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.editUser(params.row);
                    }
                  }
                },
                "修改"
              )
            );

            actions.push(
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  directives: [
                    {
                      name: "perms",
                      value: "sys:user:delete"
                    }
                  ],
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.delUser(params.row.userId);
                    }
                  }
                },
                "删除"
              )
            );

            actions.push(
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  directives: [
                    {
                      name: "perms",
                      value: "sys:user:resetpwd"
                    }
                  ],
                  on: {
                    click: () => {
                      this.resetUserPwd(params.row.userId);
                    }
                  }
                },
                "重置密码"
              )
            );

            return h("div", actions);
          }
        }
      ],
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formShow: false,
      formTitle: "添加用户",
      parentForm: {},
      delModal: false,
      delId: "", //删除的Id
      resetModal: false,
      resetId: "", //密码重置Id
      btnLoading: false,
      searchForm: {
        username: ""
      }
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.getTableList();
  },
  methods: {
    resetSearch() {},
    doSearchTableList() {
      this.currentPage = 1;
      this.getTableList();
    },
    async getTableList() {
      const params = {
        limit: 10,
        page: this.currentPage
      };
      const result = await getUserList(params);
      if (result.code == 0) {
        let res = result.data;
        this.total = res.totalCount;
        this.tableData = res.list;
      }
    },
    changeCurrentPage(num) {
      this.currentPage = num;
      this.getTableList();
    },
    addUser() {
      Object.keys(this.parentForm).forEach(key => (this.parentForm[key] = ""));
      this.formTitle = "添加用户";
      this.formShow = true;
    },
    editUser(row) {
      this.parentForm = JSON.parse(JSON.stringify(row));
      this.formTitle = "修改用户";
      this.formShow = true;
    },
    delUser(Id) {
      this.delId = Id;
      this.delModal = true;
    },
    async comfirmDel() {
      this.btnLoading = true;
      try {
        const res = await delUser([this.delId]);
        if (res.code == "0") {
          this.$Message.success("删除成功!");
          this.getTableList();
          this.delModal = false;
        } else {
          this.$Message.error(res.msg);
        }
      } catch (err) {
        console.log(err);
        this.$Message.error("服务器异常，稍后再试");
      }
      this.btnLoading = false;
    },
    resetUserPwd(Id) {
      console.log(Id);
      this.resetId = Id;
      this.resetModal = true;
    },
    async comfirmReset() {
      this.btnLoading = true;
      try {
        const res = await resetPwd({ id: this.resetId });
        if (res.code == "0") {
          this.$Message.success("重置成功!");
          this.resetModal = false;
        } else {
          this.$Message.error(res.msg);
        }
      } catch (err) {
        console.log(err);
        this.$Message.error("服务器异常，稍后再试");
      }
      this.btnLoading = false;
    },
    hideModel() {
      this.formShow = false;
    }
  }
};
</script>
